﻿using ACFaceAPI.yufan.Result;
using ACFaceAPI.yufan.Model;
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace ACFaceAPI.yufan
{
    /// <summary>
    /// 宇泛API操作类(对YuFanClient再封装)
    /// </summary>
    public static class YuFanHelper
    {
        public static YuFanClient YuFanClient { get; set; }
        public static string YuFanClientPass { get; set; }
        public static string YuFanClientURL { get; set; }

        public static string LastError { get; set; }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="url"></param>
        /// <param name="pass"></param>
        public static void Init(string url,string pass)
        {
            YuFanClient = null;
            YuFanClientURL = url;
            YuFanClientPass = pass;
            YuFanClient = new YuFanClient(YuFanClientURL, YuFanClientPass);
        }
        #region 一、设备管理类接口
        /// <summary>
        /// 设备序列号获取
        /// </summary>
        /// <returns>String</returns>
        public static string GetDeviceKey()
        {
            var r = YuFanClient.GetDeviceKey();
            if (r.success)
            {
                return r.data.ToString();
            }
            else
            {
                LastError = r.GetErrorMsg();
                return "";
            }
        }

        /// <summary>
        /// 设置设备密码
        /// </summary>
        /// <param name="oldPass">旧密码</param>
        /// <param name="newPass">新密码</param>
        /// <returns>bool</returns>
        public static bool SetDevicePass(string oldPass, string newPass)
        {
            var r = YuFanClient.SetPassword(oldPass, newPass);
            LastError = r.GetErrorMsg();
            return r.success;
        }

        /// <summary>
        /// 设置设备配置
        /// </summary>
        /// <param name="Config">配置对象实体</param>
        /// <returns></returns>
        public static YuFanResultConfig SetDeviceConfig(YuFanConfig Config)
        {
            return YuFanClient.SetDeviceConfig(Config.ToJObject()).ToYuFanResultConfig();
        }

        /// <summary>
        /// 获取设备配置
        /// </summary>
        /// <returns></returns>
        public static YuFanResultConfig GetDeviceConfig()
        {
            return YuFanClient.GetDeviceConfig().ToYuFanResultConfig();
        }
        #endregion
    }
}
